//
//  FTBaseCell.m
//  WebPDA
//
//  Created by Justin Yip on 8/15/10.
//  Copyright 2010 TenNights.com. All rights reserved.
//

#import "FTBaseCell.h"


@implementation FTBaseCell
@synthesize label;
@synthesize formBundle;
@synthesize valueHolder;
@synthesize preferenceSpecifier;

#pragma mark -
#pragma mark Cell lifecycle

-(id)initWithBundle:(NSBundle*)bundle valueHolder:(id)object reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier])) {
		//retain the bundle
		self.formBundle = bundle;
		self.valueHolder = object;
        
		self.selectionStyle = UITableViewCellSelectionStyleNone;
		
		label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 25)];
		label.textAlignment = UITextAlignmentLeft;
		label.font = [UIFont boldSystemFontOfSize:20];
		label.backgroundColor = [UIColor clearColor];
		[self.contentView addSubview:label];
    }
    return self;
}

-(void)configureWithObject:(id)object preferenceSpecifier:(NSDictionary*)ps {
	preferenceSpecifier = [ps copy];
	NSString * title= [preferenceSpecifier objectForKey:@"Title"];
	label.text = NSLocalizedStringFromTableInBundle(title, @"Root", formBundle, @"");
}

-(void)prepareForReuse {
	[super prepareForReuse];
	NSLog(@"save value %@-%@", [self getValue], [self keyFromPreferenceSpecifier]);
	[valueHolder setValue:[self getValue] forKey:[self keyFromPreferenceSpecifier]];
}

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
	[label removeFromSuperview];
	
	[label release];
	[formBundle release];
	[valueHolder release];
	[preferenceSpecifier release];
	
    [super dealloc];
}

#pragma mark -
#pragma mark Callback

-(void)didSelected:(UINavigationController *)navigationController {
	//empty implementation
}
-(id)getValue {
	return nil;
}

#pragma mark -
#pragma mark Util for subclass

-(NSString*)keyFromPreferenceSpecifier {
	if ([preferenceSpecifier objectForKey:@"Key"]) 
	{
		return [preferenceSpecifier objectForKey:@"Key"];
	}else 
	{
		return [preferenceSpecifier objectForKey:@"Title"];
	}
}

@end
